package pt.isec.a21210392.a21190220.ludotwist.GameLogic;

import java.io.Serializable;

import pt.isec.a21210392.a21190220.ludotwist.Constants;
import pt.isec.a21210392.a21190220.ludotwist.GameActivity;
import pt.isec.a21210392.a21190220.ludotwist.ToDraw;

public abstract class House implements Serializable
{
	private static final long serialVersionUID = 1L;
	private int number;
	private int normalImage;
	private double boardLine;
	private double boardColumn;
	protected Pin visitor;
	
	public House(int number, int normalImage, double line, double column)
	{
		this.number = number;
		this.normalImage = normalImage;
		this.boardLine = line;
		this.boardColumn = column;
		this.visitor = null;
	}
	public abstract int getDestiny(Pin pin, int diceResult);
	public abstract void arrivedHere(Pin pin, int diceResult);
	public int getImage()
	{
		return normalImage;
	}
	public int getNumber()
	{
		return number;
	}
	public Pin getVisitor()
	{
		return visitor;
	}
	public void addToBoardView(Pin pin)
	{
		GameActivity.boardView.removePinToDraw(new ToDraw(pin));
		if (GameActivity.socket != null && !GameActivity.justStarted)
		{			
			GameActivity.boardView.addPinToDraw(new ToDraw(pin, boardLine, boardColumn, pin.getImage()),Constants.pinStep/2);
			GameActivity.updateBoard();
		}
		else
		{
			if (GameActivity.justStarted)
			{
				GameActivity.boardView.addPinToDraw(new ToDraw(pin, boardLine, boardColumn, pin.getImage()),0);
			}
			else
			{
				GameActivity.boardView.addPinToDraw(new ToDraw(pin, boardLine, boardColumn, pin.getImage()),Constants.pinStep);
			}			
		}
	}
	public void setVisitor(Pin pin)
	{
		if (visitor != null)
		{
			if (visitor instanceof SuperPin)
			{
				((SuperPin)visitor).separatePins();
			}
			else //is a Pin
			{
				if (visitor.getColor().equals(pin.getColor()))
				{
					Pin pin2 = visitor;
					removeVisitor();
					pin2.mergeWith(pin);
					return;
				}
				visitor.putIn();
			}
		}
		visitor = pin;
	}
	public void removeVisitor()
	{
		visitor = null;
	}
	public boolean canMoveTo(Pin pin)
	{
		if (visitor != null)
		{
			if (visitor instanceof SuperPin)
			{
				if (!pin.getColor().equals(visitor.getColor())) //different colors
				{
					if (!(pin instanceof SuperPin))
					{
						return false;
					}
				}
				else //same color
				{
					return false;
				}
			}
			else
			{
				if (pin.getColor().equals(visitor.getColor())) //same color
				{
					if (pin instanceof SuperPin) //one SuperPin and one Pin
					{
						return false;
					}
				}
			}
		}
		return true;
	}
}
